package ${package}.controllor

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wushi.codegen.base.BaseControllor;
import com.wushi.codegen.base.Result;
import com.wushi.codegen.base.ResultUtil;
import com.wushi.${package}.entity.${table.name?cap_first};
import com.wushi.${package}.${table.name?cap_first}Service;
 
/**
 * 
 * <br>
 * <b>功能：</b>/${table.name}Controller<br>
 * <b>作者：</b>wushi<br>
 * <b>日期：</b> Oct 2, 2018 <br>
 * */ 
@Controller
@RequestMapping("/${table.name?uncap_first}") 
public class ${table.name?cap_first}Controller extends BaseControllor{
	
	private final static Logger log= Logger.getLogger( ${table.name?cap_first}Controller.class);
	
	// Servrice autowired
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ${table.name?cap_first}Service<entity.${table.name?cap_first}> ${table.name?uncap_first}Service;
	
	/**
	 *
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList") 
	@ResponseBody
	public Result<Map<String,Object>>  datalist(HttpServletResponse response) throws Exception{
		List<table.name?cap_first> dataList = ${table.name?uncap_first}Service.queryList();
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total", ${table.name?uncap_first}Service.queryCount());
		jsonMap.put("rows", dataList);
		return ResultUtil.success(jsonMap);
	}
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Result<String> save(${table.name?cap_first} entity,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = new HashMap<String,Object>();
		if(entity.getId()==null||StringUtils.isBlank(entity.getId().toString())){
			 ${table.name?uncap_first}Service.add(entity);
		}else{
			 ${table.name?uncap_first}Service.update(entity);
		}
		return ResultUtil.success("保存成功");
	}
	
	
	@RequestMapping("/getId")
	@ResponseBody
	public Result<Object> getId(String id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = new HashMap();
		${table.name?cap_first} entity  =  ${table.name?uncap_first}Service.queryById(id);
		if(entity  == null){
			return ResultUtil.fail("没有找到对应的记录");
		}
		return ResultUtil.success(entity);
	}
	
	
	@RequestMapping("/delete")
	public void delete(String[] id,HttpServletResponse response) throws Exception{
		${table.name?uncap_first}Service.delete(id);
		ResultUtil.success("删除成功");
			
	}

}